# Table C6: MI party-lever comparison
library(tidyverse)
library(haven)

office_type_labels <- c(
  "state_partisan_races"  =  "State Partisan Offices",
  "local_partisan_races"  =  "Local Partisan Offices",
  "state_nonpartisan_races" =  "State Non-Partisan Offices",
  "local_nonpartisan_races" =  "Local Non-Partisan Offices",
  "state_ballot_measures"  =  "State Ballot Measures", 
  "local_ballot_measures"  =  "Local Ballot Measures",
  "natl_partisan_races" =  "Federal Partisan Offices")

recode_undervote <- function(ds) {
  ds |>
    mutate(choice =
             case_when(
               choice %in% c("UNDERVOTE?",
                             "NO SELECTION",
                             "NONE OF THESE CANDIDATES",
                             "NO IMAGE FOUND") ~ "UNDERVOTE",
               choice == "NA" & office_type %in% c("local measure", "state measure") ~ "UNDERVOTE",
               choice == "NA" & 
                 county %in% c("Charlevoix", "Missaukee") ~ "UNDERVOTE",
               TRUE ~ choice)
    )
}   



# Data
dat_mi <- read_dta("tbC6_michigan-grouped.dta")

top_two <- dat_mi |> 
  count(office_type, item_id_2, choice, wt = n) |> 
  slice_max(n = 2, order_by = n, by = c(item_id_2, office_type)) |> 
  mutate(rank = rank(-n), .by = c(item_id_2, office_type)) |> 
  select(-n)

apg_federal <- dat_mi |> 
  inner_join(top_two,  by = join_by(office_type, item_id_2, choice)) |> 
  summarize(
    pct_D = sum(n*(rank == 1)*(pid == "D")) / sum(n*(pid == "D")),
    pct_R = sum(n*(rank == 1)*(pid == "R")) / sum(n*(pid == "R")),
    partisans = sum(n*(pid %in% c("D", "R"))),
    .by = c(office_type, item_id_2)) |> 
  mutate(apg = abs(pct_D - pct_R))

## just change pid --> party_lever
apg_lever <- dat_mi |> 
  inner_join(top_two,  by = join_by(office_type, item_id_2, choice)) |> 
  summarize(
    pct_D = sum(n*(rank == 1)*(party_lever == "D")) / sum(n*(party_lever == "D")),
    pct_R = sum(n*(rank == 1)*(party_lever == "R")) / sum(n*(party_lever == "R")),
    partisans = sum(n*(party_lever %in% c("D", "R"))),
    .by = c(office_type, item_id_2)) |> 
  mutate(apg = abs(pct_D - pct_R))

# averages
apg_summ <- bind_rows(
  list(lever = apg_lever, federal = apg_federal), .id = "type"
) |> 
  summarize(
    apg = mean(apg),
    voters = mean(partisans),
    contests = n(), 
    .by = c(type, office_type))

# Long data ----
gt_MI <- apg_summ |> 
  pivot_wider(id_cols = c(office_type, contests), names_from = type, values_from = c(apg, voters)) |> 
  mutate(office_type = 
           factor(office_type_labels[office_type],
                  levels = office_type_labels)) |> 
  arrange(office_type) |>
  select(matches("office"), matches("apg_"), contests, matches("voters_")) |> 
  gt::gt() |>
  gt::fmt_number(matches("apg_"),, decimals = 1, scale_by = 100) |> 
  gt::fmt_number(matches("voters_"), decimals = 0, use_seps = TRUE) |> 
  gt::cols_label("apg_lever" = "Party-lever",
                 "apg_federal" = "Federal votes",
                 "voters_federal" = "Federal votes",
                 "voters_lever" = "Party-lever",
                 "contests" = gt::html("No.<br>contests"),
                 "office_type" = "") |>
  gt::tab_spanner("APG with Party defined by:", 2:3) |>
  gt::tab_spanner("Avg No. of partisan of voters", 5:6)

gt_MI

gt_MI |> 
  gt::as_latex() |> 
  cat()
